package com.htn.gallery.web.control;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.htn.gallery.web.model.Album;
import com.htn.gallery.web.model.Photo;
import com.htn.gallery.web.utils.PicasaClient;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;

@SuppressWarnings("serial")
public class ViewAlbumAction extends ActionSupport {
	private String albumId;
	public String getAlbumId() {
		return albumId;
	}
	public void setAlbumId(String albumId) {
		this.albumId = albumId;
	}
	List<Photo> photos = new ArrayList<Photo>();
	
	public List<Photo> getPhotos() {
		return photos;
	}
	public void setPhotos(List<Photo> photos) {
		this.photos = photos;
	}
	@Override
	public String execute() {
		try {
			PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
			myService.setUserCredentials("donghaiit@gmail.com", "Ocean1980");
			albumId = ((String[]) ActionContext.getContext().getParameters().get("albumId"))[0];
			String url = "https://picasaweb.google.com/data/feed/api/user/donghaiit/albumid/" + albumId;
			System.out.println("*************" + albumId + "****************");
			URL feedUrl = new URL(url);
			AlbumFeed feed = myService.getFeed(feedUrl, AlbumFeed.class);
			for(PhotoEntry photo : feed.getPhotoEntries()) {
			    System.out.println(photo.getTitle().getPlainText());
			    photos.add(new Photo(photo.getTitle().getPlainText(),
						photo.getDescription().getPlainText(),
						photo.getMediaThumbnails().get(0).getUrl(),
						photo.getMediaEditLink().getHref()));
			}
	

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return SUCCESS;
	}
}
